<?php
session_start();
require('../conn.php');
require('../functions.php');
require('admin.php');
require_once('./wx/wxpayall.class.php');
$page = $_GET['page'];
if (is_numeric($page) == false) {
    $page = 1;
} else {
    $page = $page + 1;
}
$state = guolv($_GET['state']);
//提现数据
$row = [];
$row['id'] = guolv($_GET['id']);
$row['uid'] = guolv($_GET['uid']);
$row['money'] = guolv($_GET['money']);
$row['wx'] = guolv($_GET['wx']);
$day = date("Y-m-d", time());
if ($state == 3 && isset($row['id'])) { // 拒绝提现
    $mysql->execute("update `txdata` set `state`=3 where `id`='{$row['id']}'"); // 设置提现状态
    $res = $mysql->execute("update `user` set `gold`=`gold`+ {$row['money']} where `id`='{$row['uid']}'"); //更新帐户状态, 返还金额
    if ($res) {
        echo '<script>alert(\'操作成功，积分已返还到账户\');location.href=\'txlist0.php\'</script>';
        exit;
    }
}
if ($state == 1 && isset($row['id'])) { // 允许提现
    $result = pay($row);
//    var_dump($result);exit;
//    <return code>fail时支付失败
    if (cut($result, "<result_code>", "</result_code>") == '<![CDATA[FAIL]]>') {
        $msg = str_replace("<", "", cut($result, "<return_msg>", "</return_msg>"));
        $msg = str_replace("<", "", $msg);
        echo $row['uid'] . "单号" . $row['id'] . "提现失败.<br>" . "错误信息：" . $msg;
    } else {
        echo $row['uid'] . "提现" . $row['money'] . "成功.<br>";
        $mysql->execute("update `txdata` set `state`=2,`payday`='{$day}' where `id` ={$row['id']}");
        echo '<script>alert(\'操作成功\');location.href=\'txlist0.php\'</script>';
        exit;
    }

} ?>
<?php include('head.php') ?>
    <div class="container-fluid">
    <div class="row-fluid">
<?php
include('left.php'); ?>
    <div class="span10">
    <h1 class="page-title">等待提现</h1>

    <div class="well">
    <div id="myTabContent" class="tab-content">
    <div class="tab-pane active in" id="home">
    <div class="well">
    <div class="pull-right">
        <a href="outexcel.php" target="_blank" class="btn btn-primary">批量支付</a>
        <button class="btn"><a href="txlist0.php">首页</a></button>
        <button class="btn"><a href="?page=<?php echo $page ?>">下一页</a></button>
    </div>
    <table class="table">
        <thead>
        <tr>
            <th>单号</th>
            <th>申请时间</th>
            <th>用户ID</th>
            <th>用户名</th>
            <th>电话号码</th>
            <th>openid</th>
            <th>提现金额</th>
            <th>账号状态</th>
        </tr>
        </thead>
        <tbody>
<?php
$row = $mysql->query("select * from `txdata` where `state`=1 order by `id` desc");
$row_tx = page_array(50, $page, $row, 0);
foreach ($row_tx as $v_tx) {
    $user = $mysql->query("select * from `user` WHERE  `phone`='{$v_tx['phone']}'");
    $top = $user[0]['ban'];
    if ($top == 0) {
        $state = "<font style='color:green'>正常</font>";
    } else if ($top == 1) {
        $state = "<font style='color: blue'>禁言</font>";
    } else if ($top == -1) {
        $state = "<font style='color: red'>封号</font>";
    }
    if ($top == -1) {
        print <<<table
        <tr>
          <td><a target="_blank" href="./students.php?id={$v_tx['uid']}" title="徒弟列表">{$v_tx['id']}</a></td>
		  <td>{$v_tx['day']}</td>
		  <td><a target="_blank" href="http://app.weiyouqu.net/weiApp/user_detail.php?id={$v_tx['uid']}" title="查看用户明细">{$v_tx['uid']}</a></td>
		  <td>{$user[0]['name']}</td>
		  <td>{$v_tx['phone']}</td>
		  <td>{$v_tx['wx']}</td>
		  <td>{$v_tx['money']}</td>
		  <td>{$state}</td>
		  <td>
        </tr>
table;
    } else {
        print <<<table
        <tr>
          <td><a target="_blank" href="./students.php?id={$v_tx['uid']}" title="徒弟列表">{$v_tx['id']}</a></td>
		  <td>{$v_tx['day']}</td>
		  <td><a target="_blank" href="http://app.weiyouqu.net/weiApp/user_detail.php?id={$v_tx['uid']}" title="查看用户明细">{$v_tx['uid']}</a></td>
		  <td>{$user[0]['name']}</td>
		  <td>{$v_tx['phone']}</td>
		  <td>{$v_tx['wx']}</td>
		  <td>{$v_tx['money']}</td>
		  <td>{$state}</td>
		  <td>
		    <a class="btn" href="javascript:if(confirm('确认支付？')){window.location.href = '?state=1&id={$v_tx['id']}&money={$v_tx['money']}&uid={$v_tx['uid']}&wx={$v_tx['wx']}'}">确认</a>
			<a class="btn" href="javascript:if(confirm('确认拒绝支付？')){window.location.href = '?state=3&id={$v_tx['id']}&money={$v_tx['money']}&uid={$v_tx['uid']}&wx={$v_tx['wx']}'}">拒绝</a>
		  </td>
        </tr>
table;
                                    }
                                } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="tab-pane fade" id="profile">
                    </div>
                </div>

            </div>

        </div>
    </div>


</div>
<script src="lib/bootstrap/js/bootstrap.js"></script>
</body>
</html>